package com.ming.sps.server.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.ming.sps.server.bean.ParamMap;
import com.ming.sps.server.utils.DateTimeUtil;
import com.ming.sps.server.utils.JsonUtil;
import com.ming.sps.server.utils.MingUtil;

import ming.util.base.ConfigUtil;

//@Controller
public class ApplyBaseAction extends BaseAction {

    
    
    @RequestMapping("saveLog")
    @ResponseBody
    public String saveLog() throws Exception {
    	Map<String,Object> map  = requestParam(getRequest());
 		map.put("createtime", DateTimeUtil.getDateTime());
 		map.put("uploadstatus", "0");
 		int flag = dao.insert("t_bussiness_licenselog", map);
 		String ret = "";
 		if(flag == 0){
 			ret = retError("保存日志失败。","none");
 		}else{
 			ParamMap retMap = new ParamMap();
 			retMap.put("msg", "保存日志成功。");
 			retMap.put("sound", "none");
 			ret = JsonUtil.putToStr("success", retMap);
 		}
 		return MingUtil.encrypt(ret);
    }
    
    @RequestMapping("saveApplyFile")
    @ResponseBody
    public String saveApplyFile(@RequestParam("file") CommonsMultipartFile file,
                         @RequestParam(name = "filename", defaultValue = "") String filename,
                         @RequestParam(name = "logid", defaultValue = "") String logid) throws Exception {
        System.out.println("开始上传");
        System.out.println(filename);
        String ret = "";
        InputStream is = null;
        FileOutputStream fos = null;
        int ch = 0;
        try {
            is = file.getInputStream();
            File saveDir = new File(ConfigUtil.get("applyPath"));
            if (!saveDir.exists()) {
                saveDir.mkdir();
            }
            fos = new FileOutputStream(saveDir + File.separator + filename);
            while ((ch = is.read()) != -1) {
                fos.write(ch);
            }
            ParamMap map = new ParamMap();
            map.put("id", logid);
            map.put("uploadstatus", 1);
            dao.update("t_bussiness_licenselog", "id", map);
            ret = retSuccess("上传成功！","");
        } catch (IOException e1) {
            ret = retError("上传失败！", "operError");
            e1.printStackTrace();
        } finally {
            if (fos != null) {
                fos.close();
            }
            if (is != null) {
                is.close();
            }
        }

        return MingUtil.encrypt(ret);

    }
}
